package api

import (
	. "im/internal/common"
	"im/internal/socketIO"
	"strconv"

	"github.com/gin-gonic/gin"
)

func Broadcast(c *gin.Context) {
	context := c.Query("context")
	socketIO.Io.Emit("broadcast", context)
	c.JSON(200, gin.H{
		"context": context,
	})
}

func ping(userId int) bool {
	_, ok := socketIO.UseMap[userId]
	return ok
}

func Ping(c *gin.Context) {
	userID := c.Query("user_id")
	if userID == "" {
		Response(Error("id不能为空"))
	}
	uid, err := strconv.Atoi(userID)
	if err != nil {
		Response(Error("id异常"))
	}
	if ping(uid) {
		Response(Success("用户在线", gin.H{
			"user_id": userID,
		}))
	} else {
		Response(Error("用户不在线"))
	}
}
